package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 15:40 2021/11/12
 */

import com.liu.community.cache.TagCache;
import com.liu.community.dto.BlogDto;
import com.liu.community.dto.CommentDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.enums.CommentTypeEnum;
import com.liu.community.pojor.Blog;
import com.liu.community.pojor.Comment;
import com.liu.community.pojor.User;
import com.liu.community.server.BlogService;
import com.liu.community.server.CommentService;
import com.liu.community.utils.URLUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/publishBlog")
    public  String goCreateBlogPage(Model model){
        model.addAttribute("tags", TagCache.get());
        return  "publishBlog";
    }

    @PostMapping("/addBlog")
    public  String  addBlogAndGoListPage(@RequestParam("title") String title,
                                         @RequestParam("content") String content,
                                         @RequestParam("tag") String tag ,
                                         HttpServletRequest request,
                                         Model model){
        model.addAttribute("title",title);
        model.addAttribute("content",content);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());
        //判断输入内容是否有效
        if (title == null || "".equals(title)){
            model.addAttribute("error","标题不能为空！");
            return  "publishBlog";
        }
        if (content == null || "".equals(content)){
            model.addAttribute("error","博客内容不能为空");
            return "publishBlog";
        }
        if (tag == null || "".equals(tag)){
            model.addAttribute("error","博客技术标签不能为空");
            return "publishBlog";
        }
        //用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            model.addAttribute("error","用户还没有登录呢！");
            return "publishBlog";
        }
        //可以正常添加到数据库

        //实例化Blog对象
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setTag(tag);
        blog.setWriter(user.getId());
        blogService.createOrUpdateBlog(blog);

        model.addAttribute("resultCode","操作成功");
        return URLUtils.sendRedirectTo("/profile/blog");
    }

    @PostMapping("/updateBlog")
    public  String  updateBlogAndGoListPage(@RequestParam("title") String title,
                                         @RequestParam("content") String content,
                                         @RequestParam("tag") String tag ,
                                         HttpServletRequest request,
                                         Model model,
                                         @RequestParam("id")Integer id){
        model.addAttribute("title",title);
        model.addAttribute("content",content);
        model.addAttribute("tag",tag);
        model.addAttribute("tags", TagCache.get());
        //判断输入内容是否有效
        if (title == null || "".equals(title)){
            model.addAttribute("error","标题不能为空！");
            return  "publishBlog";
        }
        if (content == null || "".equals(content)){
            model.addAttribute("error","博客内容不能为空");
            return "publishBlog";
        }
        if (tag == null || "".equals(tag)){
            model.addAttribute("error","博客技术标签不能为空");
            return "publishBlog";
        }
        //用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            model.addAttribute("error","用户还没有登录呢！");
            return "publishBlog";
        }
        //可以正常添加到数据库

        //实例化Blog对象
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setTag(tag);
        blog.setWriter(user.getId());
        blog.setId(id);
        blogService.createOrUpdateBlog(blog);

        model.addAttribute("resultCode","操作成功");
        return URLUtils.sendRedirectTo("/profile/blog");
    }

    @GetMapping("/publishBlog/edit/{id}")
    public  String  goEditPage(@PathVariable(name = "id")Integer id,Model model){
        //获取blog
        BlogDto blog = blogService.queryBlogById(id);
        model.addAttribute("title",blog.getTitle());
        model.addAttribute("content",blog.getContent());
        model.addAttribute("tag",blog.getTag());
        model.addAttribute("id",blog.getId());

        model.addAttribute("tags", TagCache.get());
        return "updateBlog";
    }

    @GetMapping("/blog/{id}")
    public  String goBlogPage(@PathVariable(name = "id")Integer id,Model model){
        BlogDto blogDto = blogService.queryBlogById(id);
        List<BlogDto> blogDtoRelated = blogService.queryRelated(blogDto);//通过博客信息查询相关标签
        List<CommentDto> commentDtoList = commentService.queryCommentByBlogIdAndType(id);
        //增加阅读数
        blogService.increViewCount(id);
        model.addAttribute("blog",blogDto);
        model.addAttribute("relatedBlog",blogDtoRelated);
        model.addAttribute("comments",commentDtoList);
        return "blog";
    }

    @GetMapping("/blogComment/{id}")
    public  String goBlogPageOfComment(@PathVariable(name = "id")Integer id,Model model){
        //通过outerId获取parentId
        Comment comment = commentService.queryCommentByCommentId(id);
        BlogDto blogDto = null;
        List<BlogDto> blogDtoRelated = null;
        List<CommentDto> commentDtoList = null;
        if (comment.getType() == CommentTypeEnum.BLOG.getType()){
            //该博客评论是问题评论下第一个博客评论,获取博客id
             blogDto = blogService.queryBlogById(comment.getParentId());
             blogDtoRelated = blogService.queryRelated(blogDto);//通过博客信息查询相关标签
             commentDtoList = commentService.queryCommentByBlogIdAndType(comment.getParentId());
            //增加阅读数
            blogService.increViewCount(comment.getParentId());
        }else {
            Integer parentId = comment.getParentId();//评论parentid
            Comment byCommentId = commentService.queryCommentByCommentId(parentId);
            blogDto = blogService.queryBlogById(byCommentId.getParentId());
            blogDtoRelated = blogService.queryRelated(blogDto);//通过博客信息查询相关标签
             commentDtoList = commentService.queryCommentByBlogIdAndType(byCommentId.getParentId());
            //增加阅读数
            blogService.increViewCount(byCommentId.getParentId());
        }
        model.addAttribute("blog",blogDto);
        model.addAttribute("relatedBlog",blogDtoRelated);
        model.addAttribute("comments",commentDtoList);
        return "blog";
    }
}
